package com.mmall.controller.admin;

import com.mmall.common.Const;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA
 * Created By Kinsey
 * Date: 2017/12/25
 * Time: 23:30
 */
@RequestMapping("/manager/user/")
@Controller
public class UserManagerController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "login" ,method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String userName, String passWord ,HttpSession session){
        ServerResponse<User> response = userService.login(userName, passWord);
        if(response.isSuccess()){
            User user = response.getData();
            if(user.getRole()== Const.Role.ROLE_ADMIN){
                //登录为管理员
                session.setAttribute(Const.CURRENT_USER,user);
                return response;
            }else{
                return ServerResponse.createByErrorOnMsg("不是管理员，无法登录");
            }
        }
        return response;
    }

}
